﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class LocalStringResourceRepository : Data.Repository.ILocalStringResourceRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region ILocalStringResourceRepository Members

		public int CreateNewLocaleStringResource(Data.Entity.LocalStringResource theNewLocaleStringResource)
		{
			if(theNewLocaleStringResource == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_LocalStringResource_Create(theNewLocaleStringResource.LanguageID, theNewLocaleStringResource.ResourceName,
					theNewLocaleStringResource.ResourceValue);
			}

			return result;
		}

		public IList<Data.Entity.LocalStringResource> GetLocaleStringResourceByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			List<ECommerce_GetLocalStringResource_By_LanguageResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetLocalStringResource_By_Language(languageID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.LocalStringResource>();

				foreach(var lsr in lstData)
				{
					var theEntity = new Data.Entity.LocalStringResource()
					{
						LanguageID = lsr.LanguageID,
						LocaleStringResourceID = lsr.LocaleStringResourceID,
						ResourceName = lsr.ResourceName,
						ResourceValue = lsr.ResourceValue
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateLocaleStringResource(Data.Entity.LocalStringResource theLocaleStringResource)
		{
			if(theLocaleStringResource == null || theLocaleStringResource.LocaleStringResourceID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbLocalStringResource = (from lsr in _dbContext.LocalStringResources
											 where lsr.LocaleStringResourceID == theLocaleStringResource.LocaleStringResourceID
											 select lsr).FirstOrDefault();

				if(dbLocalStringResource == null)
				{
					result = 0;
				}
				else
				{
					dbLocalStringResource.LanguageID = theLocaleStringResource.LanguageID;
					dbLocalStringResource.ResourceName = theLocaleStringResource.ResourceName;
					dbLocalStringResource.ResourceValue = theLocaleStringResource.ResourceValue;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public IList<Data.Entity.LocalStringResource> GetAllLocaleStringResource()
		{
			List<Data.LocalStringResource> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.LocalStringResources;

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.LocalStringResource>();

				foreach(var lsr in lstData)
				{
					var theEntity = new Data.Entity.LocalStringResource()
					{
						LanguageID = lsr.LanguageID,
						LocaleStringResourceID = lsr.LocaleStringResourceID,
						ResourceName = lsr.ResourceName,
						ResourceValue = lsr.ResourceValue
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int DeleteLocaleStringResource(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_LocalStringResource_Delete(id);
			}

			return result;
		}

		public Data.Entity.LocalStringResource GetTheLocalStringResource(int id)
		{
			if(id <= 0)
			{
				return null;
			}

			System.Collections.Generic.List<Data.LocalStringResource> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.LocalStringResources.Where(exp => exp.LocaleStringResourceID.Equals(id));

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var lsr = lstData[0];

				Data.Entity.LocalStringResource result;

				result = new Data.Entity.LocalStringResource()
				{
					LanguageID = lsr.LanguageID,
					LocaleStringResourceID = lsr.LocaleStringResourceID,
					ResourceName = lsr.ResourceName,
					ResourceValue = lsr.ResourceValue
				};

				return result;
			}

			return null;
		}

		#endregion
	}
}
